package com.quartet.modules.vehicle.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.quartet.api.ApiResult;
import com.quartet.api.PageResult;
import com.quartet.modules.alarm.dto.TTerminalRealAlarmInfoResponseDto;
import com.quartet.modules.alarm.service.TTerminalAlarmInfoService;
import com.quartet.modules.customer.service.TCustomerInfoService;
import com.quartet.modules.system.service.DictDetailServicePs;
import com.quartet.modules.vehicle.dto.TVehicleRealAlarmInfoRequestDto;
import com.quartet.modules.vehicle.service.TVehicleAlarmInfoService;
import com.quartet.utils.LocaleUtils;
import com.quartet.utils.StringUtils;
import com.quartet.utils.constant.MapServerConstant;
import com.quartet.utils.enums.AlarmTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 终端报警信息表(TVehicleAlarmInfo)表控制层
 *
 * @author makejava
 * @since 2021-05-09 12:32:41
 */
@RestController
@RequestMapping("tTerminalAlarmInfo")
@Api(tags = "业务：终端报警信息")
@Slf4j
public class TVehicleAlarmInfoController {
    /**
     * 服务对象
     */
    @Resource
    private TVehicleAlarmInfoService tVehicleAlarmInfoService;

    /**
     * 服务对象
     */
    @Resource
    private TTerminalAlarmInfoService tOtherAlarmInfoService;

    /**
     * 服务对象
     */
    @Resource
    private TCustomerInfoService tCustomerInfoService;
    /**
     * 服务对象
     */
    @Resource
    private DictDetailServicePs dictDetailServicePs;

    /**
     * 历史报警列表查询
     *
     * @param tVehicleRealAlarmInfoRequestDto 查询实体
     * @return 所有数据
     */
    @ApiOperation("历史报警列表查询")
    @PostMapping("/selectTerminalHistoryAlarmList")
//    @AuthKeyValidated("alarm")
    public ApiResult<PageResult<TTerminalRealAlarmInfoResponseDto>> selectTerminalHistoryAlarmList(@RequestBody TVehicleRealAlarmInfoRequestDto tVehicleRealAlarmInfoRequestDto) {
        log.info("---------selectVehicleRealAlarmList begin, tVehicleRealAlarmInfoRequestDto is {}----------", tVehicleRealAlarmInfoRequestDto);
        ApiResult<PageResult<TTerminalRealAlarmInfoResponseDto>> apiResult = tOtherAlarmInfoService.selectOtherRealAlarmList(tVehicleRealAlarmInfoRequestDto);
        return apiResult;
    }

//    /**
//     * 报警次数统计
//     *
//     * @param tVehicleRealAlarmInfoRequestDto 查询实体
//     * @return 所有数据
//     */
//    @ApiOperation("报警次数统计查询")
//    @PostMapping("/selectVehicleAlarmStatisticsList")
//    @AuthKeyValidated("alarm")
//    public ApiResult<List<TVehicleAlarmInfoStatisticsResponseDto>> selectVehicleAlarmStatisticsList(@RequestBody TVehicleRealAlarmInfoRequestDto tVehicleRealAlarmInfoRequestDto) {
//        log.info("---------selectVehicleAlarmStatisticsList begin, tVehicleRealAlarmInfoRequestDto is {}----------", tVehicleRealAlarmInfoRequestDto);
//        //其他报警中包括超速和疲劳驾驶报警  放数据的时候多放一次
//        List<TVehicleAlarmInfoStatisticsResponseDto> tVehicleAlarmInfoStatisticsResponseDtoList = tOtherAlarmInfoService.selectOtherAlarmStatisticsList(tVehicleRealAlarmInfoRequestDto);
//        return ApiResult.ok(tVehicleAlarmInfoStatisticsResponseDtoList);
//    }

//    /**
//     * 安全报警处理列表查询
//     *
//     * @param tVehicleRealAlarmInfoRequestDto 查询实体
//     * @return 所有数据
//     */
//    @ApiOperation("安全报警处理列表查询")
//    @PostMapping("/selectSecurityAlarmProcessingList")
//    @AuthKeyValidated("alarm")
//    public ApiResult<PageResult<TVehicleSecurityAlarmProcessingResponseDto>> selectSecurityAlarmProcessingList(@RequestBody TVehicleRealAlarmInfoRequestDto tVehicleRealAlarmInfoRequestDto) {
//        log.info("---------selectSecurityAlarmProcessingList begin, tVehicleRealAlarmInfoRequestDto is {}----------", tVehicleRealAlarmInfoRequestDto);
//        List<TVehicleSecurityAlarmProcessingResponseDto> tVehicleSecurityAlarmProcessingResponseDtoList;
////        if (AlarmProcessModeEnum.UNPROCESSED.getValue().equals(tVehicleRealAlarmInfoRequestDto.getAlarmProcessMode())) {
//            tVehicleSecurityAlarmProcessingResponseDtoList = tOtherAlarmInfoService.selectSecurityAlarmProcessingList(tVehicleRealAlarmInfoRequestDto);
////        }
////        else {
////            tVehicleSecurityAlarmProcessingResponseDtoList = tAlarmInfoService.selectSecurityAlarmProcessingList(tVehicleRealAlarmInfoRequestDto);
////        }
//        List<TVehicleSecurityAlarmProcessingResponseDto> tVehicleSecurityAlarmProcessingResponseDtoList1 = tVehicleSecurityAlarmProcessingResponseDtoList.stream().skip(((long) tVehicleRealAlarmInfoRequestDto.getCurrent() - 1) * tVehicleRealAlarmInfoRequestDto.getLimit()).limit(tVehicleRealAlarmInfoRequestDto.getLimit()).collect(Collectors.toList());
//        PageResult pageResult = new PageResult();
//        pageResult.setTotal((long) tVehicleSecurityAlarmProcessingResponseDtoList.size());
//        pageResult.setData(tVehicleSecurityAlarmProcessingResponseDtoList1);
//        return ApiResult.ok(pageResult);
//    }

    /**
     * 批量修改报警处理状态
     *
     * @return 所有数据
     */
    @ApiOperation("修改报警处理状态")
    @PostMapping("/updateTerminalAlarmStatus")
//    @AuthKeyValidated("alarm")
    public ApiResult updateTerminalAlarmStatus(@RequestBody List<Integer> idList) {
        log.info("---------updateTerminalAlarmStatus begin, idList is {}----------", idList);
//        UpdateWrapper<TTerminalAlarm> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.lambda().in(TTerminalAlarm::getId, idList).set(TTerminalAlarm::getAlarmProcessMode, AlarmProcessModeEnum.PROCESSED.getValue());
        tOtherAlarmInfoService.updateTerminalAlarmStatus(idList);
        return ApiResult.ok();
    }

    /**
     * 历史报警详情查看
     *
     * @return 所有数据
     */
    @ApiOperation("历史报警详情查看")
    @PostMapping("/queryTerminalAlarmInfo")
//    @AuthKeyValidated("alarm")
    public ApiResult<TTerminalRealAlarmInfoResponseDto> queryTerminalAlarmInfo(@RequestParam Integer id) {
        log.info("---------queryTerminalAlarmInfo begin, id is {}----------", id);
//        TTerminalAlarm tTerminalAlarm = tOtherAlarmInfoService.getById(id);
        TTerminalRealAlarmInfoResponseDto tTerminalRealAlarmInfoResponseDto = tOtherAlarmInfoService.getAlarmInfoById(id);
        if (ObjectUtils.isEmpty(tTerminalRealAlarmInfoResponseDto)) {
            return ApiResult.ok();
        }
//        TTerminalRealAlarmInfoResponseDto tTerminalRealAlarmInfoResponseDto = BeanUtil.copyProperties(tTerminalAlarm, TTerminalRealAlarmInfoResponseDto.class);
        String warningMark = tTerminalRealAlarmInfoResponseDto.getWarningMark();
        List<Integer> alarmType = JSON.parseArray(warningMark, Integer.class);
        List<String> alarmTypeList = new ArrayList<>();
        if (LocaleUtils.getLocale().startsWith(MapServerConstant.EN)) {
            for (Integer integer : alarmType) {
                alarmTypeList.add(AlarmTypeEnum.find(integer.toString()).getDescriptionEn());
            }
        } else {
            for (Integer integer : alarmType) {
                alarmTypeList.add(AlarmTypeEnum.find(integer.toString()).getDescription());
            }
        }
        tTerminalRealAlarmInfoResponseDto.setAlarmTypeLabelList(alarmTypeList);
        return ApiResult.ok(tTerminalRealAlarmInfoResponseDto);
    }


    /**
     * 实时报警列表查询
     *
     * @return 所有数据
     */
    @ApiOperation("实时报警列表查询")
    @PostMapping("/selectTerminalRealAlarmList")
    @ApiImplicitParams(@ApiImplicitParam(name = "type", value = "不传值代表过滤数据，1代表全部数据"))
//    @AuthKeyValidated("alarm")
    public ApiResult<List<TTerminalRealAlarmInfoResponseDto>> selectTerminalRealAlarmList(@RequestParam(value = "type", required = false) String type) {
//        ApiResult<PageResult<TTerminalRealAlarmInfoResponseDto>> apiResult = tOtherAlarmInfoService.selectOtherRealAlarmList(tVehicleRealAlarmInfoRequestDto);
//        QueryWrapper<TTerminalAlarm> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(TTerminalAlarm::getAlarmProcessMode, AlarmProcessModeEnum.UNPROCESSED.getValue());
        List<String> warningMarkList = null;
        if (StringUtils.isBlank(type)) {
            List<Integer> alarmTypeList = MapServerConstant.alarmTypeList;
            warningMarkList = alarmTypeList.stream().map(s -> String.valueOf(s)).collect(Collectors.toList());
        }
        List<TTerminalRealAlarmInfoResponseDto> tTerminalRealAlarmInfoResponseDtoList = tOtherAlarmInfoService.selectTerminalRealAlarmList(warningMarkList);
//        List<TTerminalRealAlarmInfoResponseDto> tTerminalRealAlarmInfoResponseDtoList = BeanTool.copyObjListProperties(tTerminalAlarmList, TTerminalRealAlarmInfoResponseDto.class);
        List<TTerminalRealAlarmInfoResponseDto> tTerminalRealAlarmInfoResponseDtoList1 = new ArrayList<>();
        tTerminalRealAlarmInfoResponseDtoList.forEach(tTerminalRealAlarmInfoResponseDto -> {
            //将设备时间注入到创建时间中  前端用的创建时间展示的报警时间
            tTerminalRealAlarmInfoResponseDto.setCreateTime(tTerminalRealAlarmInfoResponseDto.getDeviceTime());
            String warningMark = tTerminalRealAlarmInfoResponseDto.getWarningMark();
            List<Integer> alarmType = JSON.parseArray(warningMark, Integer.class);
            for (Integer integer : alarmType) {
                if (!MapServerConstant.alarmTypeList.contains(integer)){
                    continue;
                }
                TTerminalRealAlarmInfoResponseDto tTerminalRealAlarmInfoResponseDto1 = BeanUtil.copyProperties(tTerminalRealAlarmInfoResponseDto, TTerminalRealAlarmInfoResponseDto.class);
                if (LocaleUtils.getLocale().startsWith(MapServerConstant.EN)) {
                    tTerminalRealAlarmInfoResponseDto1.setAlarmTypeLabel(AlarmTypeEnum.find(integer.toString()).getDescriptionEn());
                } else {
                    tTerminalRealAlarmInfoResponseDto1.setAlarmTypeLabel(AlarmTypeEnum.find(integer.toString()).getDescription());
                }
                tTerminalRealAlarmInfoResponseDtoList1.add(tTerminalRealAlarmInfoResponseDto1);
            }
//            String label = dictDetailServicePs.getLabelByValueByCodeAndName(list.get(0).toString(), "alarmType");

        });
        return ApiResult.ok(tTerminalRealAlarmInfoResponseDtoList1);
    }

    /**
     * 实时报警个数查询
     *
     * @return 所有数据
     */
    @ApiOperation("实时报警个数查询")
    @PostMapping("/selectTerminalRealAlarmCount")
    @ApiImplicitParams(@ApiImplicitParam(name = "type", value = "不传值代表过滤数据，1代表全部数据"))
//    @AuthKeyValidated("alarm")
    public ApiResult<Integer> selectTerminalRealAlarmCount(@RequestParam(value = "type", required = false) String type) {
        List<String> warningMarkList = null;
        if (StringUtils.isBlank(type)) {
            List<Integer> alarmTypeList = MapServerConstant.alarmTypeList;
            warningMarkList = alarmTypeList.stream().map(s -> String.valueOf(s)).collect(Collectors.toList());
        }
        List<TTerminalRealAlarmInfoResponseDto> tTerminalRealAlarmInfoResponseDtoList = tOtherAlarmInfoService.selectTerminalRealAlarmList(warningMarkList);
//        List<TTerminalRealAlarmInfoResponseDto> tTerminalRealAlarmInfoResponseDtoList1 = new ArrayList<>();
        Integer count = tTerminalRealAlarmInfoResponseDtoList.stream().map(tTerminalRealAlarmInfoResponseDto -> {
            String warningMark = tTerminalRealAlarmInfoResponseDto.getWarningMark();
            List<Integer> list = JSON.parseArray(warningMark, Integer.class);
            Long count1 = list.stream().filter(integer -> MapServerConstant.alarmTypeList.contains(integer)).count();
            return count1.intValue();
        }).mapToInt(Integer::intValue).sum();
        return ApiResult.ok(count);
    }
}
